package com.rtline.business.service.impl;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.rtline.business.service.UserOperationStrategy;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>UserOperationStrategyFactory .
 *
 * <p>change history:
 *
 * <pre>
 * date         defect       person          comments
 * --------------------------------------------------------------------------------
 * 2025/5/31    ********     Tao Ren    create file.
 * </pre>
 *
 * @author Tao Ren
 * @date 2025/5/31 13:45
 */
@Service
public class UserOperationStrategy2Factory implements InitializingBean {

    @Autowired
    private List<UserOperationStrategy> serviceList;

    private Map<String, UserOperationStrategy> strategies;

    @Override
    // 项目启动时将实现类放入到map中去
    public void afterPropertiesSet() throws Exception {
        strategies = serviceList.stream().collect(Collectors.toMap(UserOperationStrategy::strategyCode, Function.identity()));
    }

    public UserOperationStrategy getStrategy(String operationType) {
        return strategies.get(operationType);
    }
}
